<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Each</title>
    <script src="../jquery-3.4.1.js"></script>
    <style>
        .left {
            width: auto;
            height: 150px;
        }
        .left div {
            width: 150px;
            height: 120px;
            padding: 5px;
            margin: 5px;
            float: left;
            background: #bbffaa;
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
    <h2>each方法</h2>
    <div class="left first-div">
        <div class="div">
            <ul>
                <li>list item 1</li>
                <li>list item 2</li>
                <li>list item 3</li>
            </ul>
        </div>
        <div class="div">
            <ul>
                <li>list item 4</li>
                <li>list item 5</li>
                <li>list item 6</li>
            </ul>
        </div>
    </div>

    <br/>
    <button>each遍历元素</button>
    <button>each回调判断</button>
</body>
<script>
    $("button:first").click(function () {
        $("li").each(function (index,element) {
            $(this).css('color','red')
        })
    });

    $("button:last").click(function () {
        $("li").each(function (index,element) {
            if(index % 2){
                $(this).css('color',"red")
            }
        });
    })
</script>
</html>